#include<iostream> #include<stdio.h> #include<vector> using namespace std; const int NUM=10; vector<int,int> getSmall(double a[][NUM]) { int main(void) { double a[][NUM]; for(int i=0;i<NUM;i++) { for(int j=0;j<NUM;j++) { cout<<"please enter the P"<<i<<j<<endl; cin>>a[i][j]; } }
#include<stdio.h> #include<stdlib.h> #include<fstream> #include<iostream> using namespace std; const int NUM=2; struct pai { int x; int y; double v; }; pai* getmin(double (*a)[2],int col,int row) { //struct pair *p; pai pp; pp.v=100,pp.x=111; pp.y=100; pai* p=&pp; double temp=1000; for(int i=0;i<col;i++) for(int j=0;j<row;j++) { if(temp>a[i][j]) { temp=a[i][j]; p->x=i; p->y=j; p->v=temp; } } return p; } int * get(double (*a)[2],int col,int row) { //struct pair *p; int count=0; int pp[2]={9}; int* ppp=pp; double temp=1000; for(int i=0;i<col;i++) for(int j=0;j<row;j++) { if(temp>a[i][j]) { count++; temp=a[i][j]; ppp[0]=i; ppp[1]=j; } } if(count==0) return NULL; return ppp; } int main(void) { double a[][2]={0.1,0.4,0.5,0.02}; int b[2]={0}; //double p=getmin(a,2,2); pai pp; pp.v=0; pp.x=0; pp.y=0; int bb[4]={-1}; int row=0; for(int i=0;i<NUM;i++) { //pai *p=getmin(a,2,2); int *pt=get(a,2,2); for(int ii=0;ii<2;ii++) for(int jj=0;jj<2;jj++) { if(ii==pt[0]||jj==pt[1]) a[ii][jj]=100; } //cout<<p->x<<" "<<p->y<<endl; cout<<pt[1]<<" ?"<<pt[0]<<endl; bb[row]=pt[0]; bb[row+1]=pt[1]; row+=2; } //cout<<p<<endl; return 0; }
You need to enable Javascript in your browser to edit pages.
help on how to format text
工作分配
n个人,n份工作,每个人只能做一样工作,第i个人(i=1...n)做第j个工作的费用是Pij,求整体的最小花费:我们假设这n个人,n份工作对应一个n*n的矩阵:
1, 2, 3, ...n
1 [ P11,P12, ...P1n
2 P21,p22,... P2n
... ... ... ...
n Pn1,Pn2,... Pnn]
对于这个问题我们提出以下方法:
逐次查找矩阵的最小元素,找到第一个最小元素标记为Pij,记录下标i,j,然后配置矩阵中的第i行和第j列的元素为某个相当大的数字(这个数字
大于矩阵中的最大的数字)然后查找新的矩阵中的最小元素,按照上面的步骤逐次迭代n-1次,最后矩阵变成一个所有元素都相同的矩阵,
查找记录下来的下标:
代码实现:
Dec 22, 2010 7:09 pm
#include<stdio.h> #include<stdlib.h> #include<fstream> #include<iostream> using namespace std; const int NUM=2; struct pai { int x; int y; double v; }; pai* getmin(double (*a)[2],int col,int row) { //struct pair *p; pai pp; pp.v=100,pp.x=111; pp.y=100; pai* p=&pp; double temp=1000; for(int i=0;i<col;i++) for(int j=0;j<row;j++) { if(temp>a[i][j]) { temp=a[i][j]; p->x=i; p->y=j; p->v=temp; } } return p; } int * get(double (*a)[2],int col,int row) { //struct pair *p; int count=0; int pp[2]={9}; int* ppp=pp; double temp=1000; for(int i=0;i<col;i++) for(int j=0;j<row;j++) { if(temp>a[i][j]) { count++; temp=a[i][j]; ppp[0]=i; ppp[1]=j; } } if(count==0) return NULL; return ppp; } int main(void) { double a[][2]={0.1,0.4,0.5,0.02}; int b[2]={0}; //double p=getmin(a,2,2); pai pp; pp.v=0; pp.x=0; pp.y=0; int bb[4]={-1}; int row=0; for(int i=0;i<NUM;i++) { //pai *p=getmin(a,2,2); int *pt=get(a,2,2); for(int ii=0;ii<2;ii++) for(int jj=0;jj<2;jj++) { if(ii==pt[0]||jj==pt[1]) a[ii][jj]=100; } //cout<<p->x<<" "<<p->y<<endl; cout<<pt[1]<<" ?"<<pt[0]<<endl; bb[row]=pt[0]; bb[row+1]=pt[1]; row+=2; } //cout<<p<<endl; return 0; }--
Dec 22, 2010 7:09 pm